<?php
//菜单，接口权限业务模块
class AuthService extends TP_Service
{
    private $action_whiteList = array("__construct", "doTransaction", "get_instance");
    public function __construct()
    {
        parent::__construct();
        $this->load->Model("PModel/Action_model");
        $this->load->Model("PModel/Account_model");
        $this->load->Model("PModel/Admin_model");
        $this->load->Model("PModel/Role_model");
        $this->load->Model("PModel/Menurole_model");
        $this->load->Model("PModel/Actionrole_model");
    }

    /**
     * saveMenus function
     *
     * @Name 保存菜单
     * @Desc 方法描述
     * @Author Pekey
     * @Date 2020-08-13
     * @param [type] $roleid
     * @param [type] $menus
     * @return void
     */
    public function saveMenus($roleid, $menus)
    {
        //为空判断
        api_params_check($roleid, $menus);

        $data = array();
        $data['role_id'] = $roleid;
        $data['menu_data'] = $menus;
        $this->Menurole_model->delete(array("role_id" => $roleid));
        $this->Menurole_model->add($data);
    }

    /**
     * generateAction function
     *
     * @Name generateAction
     * @Desc 获得控制器所有有效方法
     * @Author Pekey
     * @Date 2020-08-31
     * @return void
     */
    private function generateAction()
    {
        //遍历所有控制器下目录
        //controller下面的文件夹集合
        $dirs = array();
        $dirs = glob('application/controllers/admin/*');
        //控制器集合
        $pathList = array();
        //action方法集合
        $action_datas = array();

        //系统中所有控制的方法,遍历所有控制器。
        foreach ($dirs as $key => $v) {
            //如果主目录中有以php结尾的类，加入类主别中
            if (strpos($v, 'Controller.php') !== false) {
                array_push($pathList, $v);
                continue;
            }
            //如果是其他.结尾，直接跳过
            if (strpos($v, '.') !== false) {
                continue;
            }

            $pathList = array_merge($pathList, glob($v . '/*Controller.php'));
        }

        foreach ($pathList as $key => $value) {
            require_once $value;
            $controller = basename($value, '.php');
            echo '【' . $controller . '】';
            $methods = get_class_methods($controller);
            if ($methods == null || count($methods) <= 0) {
                continue;
            }

            //通过反映射获取注释信息
            $reflection = new ReflectionClass($controller);
            $model=dealPageComment($reflection->getDocComment());
            foreach ($methods as $key1 => $method) {
                //在白名单，不需要检查的
                if (in_array($method, $this->action_whiteList)) {
                    continue;
                }
                // echo $method;
                $method_temp = $reflection->getMethod($method);

                //非公开的public中的,不需要检查
                if (!$method_temp->isPublic()) {
                    continue;
                }

                echo '(' . $method . ')';

                $comment = $method_temp->getDocComment();
                $comment_datas = array();
                $comment_datas = array_merge($comment_datas, dealComment($comment));
                $comment_datas['path'] = substr($value, 24, strlen($value) - 4 - 24) . '/' . $method;
                // 2021-01-05新增模块 用于区分接口类型
                $comment_datas['model']=$model;
                array_push($action_datas, $comment_datas);

            }
        }
        return $action_datas;
    }

    /**
     * updateAction function
     *
     * @Name updateAction
     * @Desc 删除原先action数据，新增新action数据
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author Pekey
     * @Date 2020-08-31
     * @return void
     */
    public function updateAction()
    {
        $datas = $this->generateAction();
        log_json($datas);
        //删除所有数据
        $this->Action_model->delete(array());
        $this->Action_model->add($datas);
    }

    /**
     * getActionDatas function
     *
     * @Name getActionDatas
     * @Desc 方法描述
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author Pekey
     * @Date 2020-09-03
     * @return void
     */
    public function getActionDatas($role)
    {
        $ret['datas'] = $this->Action_model->get_list(array());
        $ret['role'] = $this->Actionrole_model->getActionByRole($role);
        return $ret;
    }

    /**
     * getActionList function
     * 
     * @Name getActionList
     * @Desc 获取action列表
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      LiAnXu
     * @Date 2020-11-26 13:47:04 星期四
     * @return array()
     */
    public function getActionList()
    {
        $ret= $this->Action_model->get_list(array());
        foreach ($ret as $k => $v) {
            $title[$v['model']]=array('path'=>$v['model'],'id'=>$v['id'],'desc'=>$v['desc']);
        }
        foreach ($title as $k => $v) {
            $item=array('path'=>$v['path'],'id'=>$v['id']);
            $children=array();
            foreach ($ret as $k1 => $v1) {
                if($v['path']==$v1['model']){
                    $children[]=array('id'=>$v1['id'],'path'=>$v1['path'],'desc'=>$v1['desc']);
                }
                $item['children']=$children;
            }
            $data[]=$item;
        }
        return $data;
    }

    /**
     * getActionRoleList function
     * 
     * @Name getActionRoleList
     * @Desc 获取指定role下action列表
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      LiAnXu
     * @Date 2020-11-26 13:52:11 星期四
     * @return      [type]                   [description]
     */
    public function getActionRoleList($role)
    {
        $ret= $this->Actionrole_model->getActionByRole($role);
        return $ret;
    }

   /**
    * saveRoleAction function
    * 
    * @Name saveRoleAction
    * @Desc 保存指定角色action
    * @OpenFlag 是否开放,1开放0权限控制
    * @Author      LiAnXu
    * @Date 2020-11-25 10:42:02 星期三
    * @return      [type]                   [description]
    */
    public function saveRoleAction($role,$action)
    {
        $this->Actionrole_model->saveActionByRole($role,$action);
    }

    /**
     * [ getRoleList]
     * @Name        getRoleList
     * @Description 获取角色列表
     * @Author      LiAnXu
     * @Date 2020-12-11 12:52:43 星期五
     * @return      [type]                   [description]
     */
    public function getRoleList($param,$export=false){
        if(!$export){
            api_params_check($param['pageNo'],$param['pageSize']);
        }            
        $param=filterNull($param);
        $list=$this->Role_model->getList($param,$export);
        return $list;
    }

    /**
     * [ saveRole]
     * @Name        saveRole
     * @Description 保存角色
     * @Author      LiAnXu
     * @Date 2020-12-11 13:00:25 星期五
     * @return      [type]                   [description]
     */
    public function saveRole($data){
        if(!isset($data['name'])){
            exit(retJson(ErrCode::ErrCode_10002));
        }
        api_params_check($data['name']);
        $param=copyArray($data,array('name','id'));
        if(isEmpty($data['id'])){
            // 判断角色名是否重复
            $check=$this->Role_model->get_single(array('id'=>$data['id']));
            if($check){
                exit(retJson(ErrCode::ErrCode_10501));
            }
            // 新建角色
            $role=array(
                'name'=>$data['name']
            );
            $this->Role_model->add($role);
        }else{
            $where=array('id'=>$data['id']);
            $this->Role_model->updateInfo($param,$where);
        }       
        return true;
    }

    /**
     * [ setBatchRole]
     * @Name        setBatchRole
     * @Description 设置角色
     * @Author      LiAnXu
     * @Date 2020-12-11 13:54:09 星期五
     * @param  $role_id     role_id    
     * @param  $list        用户数据     
     * @param  $add        是否添加
     * @return      [type]                   [description]
     */
    public function setBatchRole($role_id,$list,$action)
    {   
        switch ($action) {
            case 'add':
                $this->AccRole_model->setRole($role_id,$list,true);
                break;
            case 'delete':
                $this->AccRole_model->setRole($role_id,$list,false);
                break;
            default:
                exit(retJson(ErrCode::ErrCode_10003));
                break;
        }
        
        return true;
    }
    
    /**
     * [ description]
     * @Name        delRole
     * @Description 删除角色
     * @Author      LiAnXu
     * @Date 2020-12-15 15:52:44 星期二
     * @param  $id  角色id
     * @return      [type]                   [description]
     */
    public function delRole($id)
    {
        $list=$this->AccRole_model->get_list(array('is_delete'=>DeleteState::UnDelete[TPEnum::Value],'role_id'=>$id));
        if($id==1){
            exit(retJson(ErrCode::ErrCode_10503));
        }
        if($list){
            exit(retJson(ErrCode::ErrCode_10502));
        }
        $this->Role_model->update(array('is_delete'=>DeleteState::Delete[TPEnum::Value]),array('id'=>$id));
        return true;
    }

    

    /**
     * [ getAdminByRole]
     * @Name        getAdminByRole
     * @Description 获取该角色下所有账户
     * @Author      LiAnXu
     * @Date 2020-12-15 16:09:20 星期二
     * @return      [type]                   [description]
     */
    public function getAdminByRole($role_id)
    {
        $data=$this->Role_model->get_list(array('id'=>$role_id,'is_delete'=>DeleteState::UnDelete[TPEnum::Value]));
        return $data;
    }

}
